package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.CompanyService;
import redis.clients.jedis.Jedis;

/**
 * redis操作
 * 1.下载redis驱动包
 */
public class CompanyServiceImpl implements CompanyService {
    @Override
    public CompanyModel getCompanyModel() {
        CompanyModel companyModel = new CompanyModel();
//        Jedis()如果没有参数，表示默认 地址是127.0.0.1，默认端口是6379，默认没有用户名或者密码
        Jedis jedis = new Jedis();
//        判断当前数据是否存在,可以直接通过key值来判断

        boolean isHave = jedis.exists("logo");
        if (isHave) {
//            在redis里面取值，并将值赋值给companyModel
//            String aa = jedis.get("aa");
            companyModel.setLogo(jedis.get("logo"));
            companyModel.setPhone(jedis.get("phone"));
            companyModel.setAddress(jedis.get("address"));
            companyModel.setNet(jedis.get("net"));
            companyModel.setLinePhone(jedis.get("linePhone"));
            companyModel.setEmail(jedis.get("email"));
            companyModel.setVersion(jedis.get("version"));

        } else {
//            如果没有，则取数据库的值，将值赋值给reids
            CompanyDao companyDao = new CompanyImpl();
            companyModel = companyDao.getCompanyModel();
//            不建议这里直接转换为json对象，因为取值的时候还要转换为我们需要的对象
//            String aa="{logo:"",phone:""}";
//            jedis.set("aa",aa);
            jedis.set("logo", companyModel.getLogo());
            jedis.set("phone", companyModel.getPhone());
            jedis.set("net", companyModel.getNet());
            jedis.set("address", companyModel.getAddress());
            jedis.set("email", companyModel.getEmail());
            jedis.set("linePhone", companyModel.getLinePhone());
            jedis.set("version", companyModel.getVersion());


        }
        return companyModel;

    }
}
